package cn.edu.cug.cs.gtl.gsm.jdbc;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据传入的Java类，生成与该类对应的数据表的SQL语句
 */
public class TableCreator {

    /*

     */
    public static Map<String,String> getJavaTypeMap(){
        Map<String,String> map = new HashMap<>();
        //字符
        map.put("String","VARCHAR");
        map.put("char","CHAR");
        //整数
        map.put("byte","INTEGER");
        map.put("java.lang.Byte","INTEGER");
        map.put("short","INTEGER");
        map.put("java.lang.Short","INTEGER");
        map.put("int","INTEGER");
        map.put("java.lang.Integer","INTEGER");
        map.put("java.lang.Long","BIGINT");
        map.put("long","BIGINT");
        //浮点数
        map.put("float","REAL");
        map.put("java.lang.Float","REAL");
        map.put("double","DOUBLE");
        map.put("java.lang.Double","DOUBLE");
        //时间
        map.put("java.sql.Date","DATE");
        map.put("java.sql.Time","TIME");
        map.put("java.sql.Timestamp","TIMESTAMP");
        //布尔
        map.put("java.sql.Boolean","BOOLEAN");
        map.put("boolean","BOOLEAN");
        //LOB
        map.put("java.sql.Boolean","BLOB");
        map.put("[B","BLOB");

        return map;
    }
    public static List<Map<String,String>> getFields(Object o){
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];

        List<Map<String,String>> list = new ArrayList<>();

        Map infoMap = null;

        for (int i = 0; i < fieldNames.length; i++) {
            infoMap = new HashMap();

            infoMap.put("type", fields[i].getType().toString());

            infoMap.put("name", fields[i].getName());

            list.add(infoMap);
        }
        return list;
    }
}
